#include "demoforgetissuewidget.h"
#include "ui_demoforgetissuewidget.h"

#include <QDebug>
#include <QGridLayout>

#include <OEMessageBox>
#include <auditcore.h>

DemoForgetIssueWidget::DemoForgetIssueWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DemoForgetIssueWidget)
{
    ui->setupUi(this);

    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quote_validator = new QRegExpValidator(regQuote, this);
    QRegExp regQuote1("^[\u4e00-\u9fa50-9a-zA-Z]{0,12}$");
    QRegExpValidator* quote_validator1 = new QRegExpValidator(regQuote1, this);
    ui->edit_new_pswd->setValidator(quote_validator);
    ui->edit_new_pswd1->setValidator(quote_validator);
    ui->edit_new_pswd->setEchoMode(QLineEdit::EchoMode::Password);
    ui->edit_new_pswd1->setEchoMode(QLineEdit::EchoMode::Password);
    ui->edt_user->setValidator(quote_validator);
    ui->edt_issue1->setValidator(quote_validator1);
    ui->edt_issue2->setValidator(quote_validator1);



    QGridLayout* layout = new QGridLayout();
    layout->addWidget(ui->stackedWidget);
    layout->setMargin(0);
    setLayout(layout);
}

DemoForgetIssueWidget::~DemoForgetIssueWidget()
{
    delete ui;
}


void DemoForgetIssueWidget::on_btn_next2_clicked()
{
    QString user = ui->edt_user->text();
    QString issue1 = ui->lab_issue1->text();
    QString answer1 = ui->edt_issue1->text();
    QString issue2 = ui->lab_issue2->text();
    QString answer2 = ui->edt_issue2->text();
    int ret = apiCheckAccountInfo(user.toUtf8().data(),issue1.toUtf8().data(),answer1.toUtf8().data(),
                        issue2.toUtf8().data(),answer2.toUtf8().data());

    if (ret != 0) {
        OEMessageBox::BriefMessage(this, tr("密保问题错误"));
        ui->edt_issue1->setFocus();
        return;
    }
    ui->stackedWidget->setCurrentIndex(2);
}
void DemoForgetIssueWidget::on_btn_next1_clicked()
{
    QString user = ui->edt_user->text();
    char ch_username[256] = {};
    char ch_issue1[256] = {};
    char ch_issue2[256] = {};

    if (user.isEmpty() || user.length() < 6) {
        OEMessageBox::BriefMessage(this, tr("账号不符合要求"));
        return;
    }

    int ret = apiGetAccountInfo(user.toUtf8().data(),ch_username, ch_issue1, ch_issue2);

    if (ret != 0) {
        OEMessageBox::BriefMessage(this, tr("账号信息错误"));
        return;
    }
    ui->lab_name->setText(QString::fromUtf8(ch_username));
    ui->lab_user->setText(user);
    ui->lab_issue1->setText(QString::fromUtf8(ch_issue1));
    ui->lab_issue2->setText(QString::fromUtf8(ch_issue2));


    ui->stackedWidget->setCurrentIndex(1);
}

void DemoForgetIssueWidget::on_btn_confirm_clicked()
{
    QString new_pswd = ui->edit_new_pswd->text();
    QString new_pswd1 = ui->edit_new_pswd1->text();
    if (new_pswd.length() < 6) {
        OEMessageBox::BriefMessage(this, tr("密码不能小于6位"));
        ui->edit_new_pswd->setFocus();
        return;
    }
    else if (new_pswd != new_pswd1) {
        OEMessageBox::BriefMessage(this, tr("输入密码不一致"));
        ui->edit_new_pswd1->setFocus();
        return;
    }

    QString user = ui->edt_user->text();

    int ret = apiModifyAccountPswd(user.toUtf8().data(),new_pswd.toUtf8().data(),"");

    if (ret != 0) {
        OEMessageBox::BriefMessage(this, tr("修改失败"));
        return;
    }

    emit sigGetBackUser(user);
    return;
}

void DemoForgetIssueWidget::on_CloseButton_clicked()
{
    emit sigGetBackUser();
}
